﻿using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.Helper;
using System;
using System.IO;
using System.Windows.Forms;

namespace CatUtils.Utils.Currency.IoUtils
{/// <summary>
 /// 文件夹帮助
 /// </summary>
    public static class FloderHelper
    {
        /// <summary>
        /// 用户选择目录记忆保存路径 
        /// </summary>
        public static string SelectFloderInfoTemp { get; set; } = StaticValue.TempPathSystem + "SelectFloderInfoTemp.txt";
        /// <summary>
        /// 初始化系统根目录
        /// </summary>
        public static void InitFloder()
        {
            FloderExits(StaticValue.BinImportPath, true);
            FloderExits(StaticValue.LogFloder, true);
            FloderExits(StaticValue.BinViewPath, true);
        }

        /// <summary>
        ///打开文件夹 并且判断是否存在
        /// </summary>
        /// <param name="FloderPath">文件夹路径</param>
        /// <param name="CreateFloder">如果文件夹不存在则创建</param>
        public static void OpenFloder(string FloderPath, bool CreateFloder)
        {
            FloderExits(FloderPath, true);
            OpenFloder(FloderPath);
        }

        /// <summary>
        /// 打开文件夹
        /// </summary>
        /// <param name="FloderPath">文件夹路径</param>
        public static void OpenFloder(string FloderPath)
        {
            if (TextHelper.JudgeNull(FloderPath))
            {
                PrintLog.E("要打开的文件夹路径为空。");
                return;
            }
            System.Diagnostics.Process.Start(FloderPath);
        }

        /// <summary>
        /// 删除这个文件夹里所有子文件
        /// </summary>
        /// <param name="FloderPath"></param>
        public static void DeleteAllChildrenFile(string FloderPath)
        {
            foreach (string line in FloderNameHelper.GetFullFileNameList(FloderPath))
            {
                FileHelper.FileDelete(line);
            }
        }

        /// <summary>
        /// 判断文件夹是否存在
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="CreatFlag"></param>
        /// <returns></returns>
        public static bool FloderExits(string FloderPath, bool CreatFlag)
        {
            if (TextHelper.JudgeNull(FloderPath))
            {
                PrintLog.E("要创建的文件夹路径为空。");
                return false;
            }
            if (!Directory.Exists(FloderPath))
            {
                if (!CreatFlag)
                {
                    return false;
                }

                Console.WriteLine("已创建" + FloderPath);
                try { Directory.CreateDirectory(FloderPath); }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                    PrintLog.Log("异常文件夹名字:" + FloderPath);
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 删除文件夹  包括里面的文件
        /// </summary>
        /// <param name="FloderPath"></param>
        public static void FolderDelete(string FloderPath)
        {
            try
            {
                Directory.Delete(FloderPath, true);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 创建一个文件夹
        /// </summary>
        /// <param name="FloderPath"></param>
        public static void FolderCreate(string FloderPath)
        {
            try
            {
                Directory.CreateDirectory(FloderPath);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 移动文件夹文件到新文件夹
        /// </summary>
        /// <param name="OldFloder"></param>
        /// <param name="NewFloder"></param>
        public static void FloderFileMove(string OldFloder, string NewFloder)
        {
            if (!FloderExits(NewFloder, true))
            {
                Console.WriteLine("创建新文件夹失败");
                return;
            }
            foreach (string line in FloderNameHelper.GetFullFileNameList(OldFloder))
            {
                FileHelper.FileMove(line, $"{ Path.GetFullPath(NewFloder)}\\{Path.GetFileName(line)}", true);
            }
        }

        /// <summary>
        /// 移动文件到子文件夹
        /// </summary>
        /// <param name="FilePath">原始文件路径。例如：c:\\cat\232.txt</param>
        /// <param name="ChildFloderName">例如 ：child</param>
        public static void MoveToChildFloder(string FilePath, string ChildFloderName)
        {

            string FloderPath = FloderNameHelper.GetFloderPath(FilePath) + ChildFloderName + "\\";

            FloderExits(FloderPath, true);
            string NewFileName = FloderPath + FileNameHelper.GetFileName(FilePath);
            try
            {
                // 确认目标文件不存在
                if (File.Exists(FloderPath))
                {
                    File.Delete(FloderPath);
                }
                File.Move(FilePath, NewFileName);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 选择一个文件夹
        /// </summary>
        /// <returns></returns>
        public static string SelectFloder()
        {
            using (FolderBrowserDialog SelectFileFolder = new FolderBrowserDialog())
            {
                SelectFileFolder.SelectedPath = FileHelper.ReadAllText(SelectFloderInfoTemp);
                if (SelectFileFolder.ShowDialog() == DialogResult.OK)
                {
                    string SelectPath = SelectFileFolder.SelectedPath + "\\";
                    FileHelper.WriteText(SelectFloderInfoTemp, SelectPath);
                    return SelectPath;
                }
                else
                    return "";
            }
        }

        /// <summary>
        /// 移动文件夹
        /// </summary>
        /// <param name="SrcPath">来源路径</param>
        /// <param name="AimPath">目标路径</param>
        public static void Move(string SrcPath, string AimPath)
        {
            Copy(SrcPath, AimPath);
            FolderDelete(SrcPath);
        }

        /// <summary>
        /// 拷贝文件夹
        /// </summary>
        /// <param name="SrcPath">来源路径</param>
        /// <param name="AimPath">目标路径</param>
        public static void Copy(string SrcPath, string AimPath)
        {
            try
            {
                // 检查目标目录是否以目录分割字符结束如果不是则添加之
                if (AimPath[AimPath.Length - 1] != Path.DirectorySeparatorChar)
                    AimPath += Path.DirectorySeparatorChar;
                // 判断目标目录是否存在如果不存在则新建之
                if (!Directory.Exists(AimPath))
                    Directory.CreateDirectory(AimPath);
                // 得到源目录的文件列表，该里面是包含文件以及目录路径的一个数组
                // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法

                string[] fileList = Directory.GetFileSystemEntries(SrcPath);
                // 遍历所有的文件和目录
                foreach (string file in fileList)
                {
                    // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                    if (Directory.Exists(file))
                        Copy(file, AimPath + Path.GetFileName(file));
                    // 否则直接Copy文件
                    else
                        File.Copy(file, AimPath + Path.GetFileName(file), true);
                }
            }
            catch
            {
                Console.WriteLine("无法复制!");
            }
        }
    }
}